package com.example.administrator.student4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.administrator.student4.Adapter.SubjectAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**ISO-8859-1n
 * Created by Administrator on 2016/12/12.
 */
public class SubjectDetail extends Activity {
    EditText subject,teacher;
    Spinner term;
    String subject_s,teacher_s,term_s;
    Button comfirm;
    int id;
    String content[] = {"term one","term two"};
    String path = "http://10.0.2.2:8080/andorid/subject?request=";
    String a;
    int becon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subjectdetail);
        uiinit();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SubjectDetail.this, android.R.layout.simple_list_item_activated_1, content);
        term.setAdapter(adapter);
        becon = bundle.getInt("becon");
        if (becon == 1) {
            subject_s = bundle.getString("subject");
            teacher_s = bundle.getString("teacher");
            term_s = bundle.getString("term");
            id = bundle.getInt("id");
            subject.setText(subject_s);
            teacher.setText(teacher_s);
            comfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            try {
                                URL url = new URL(path + "update");
                                JSONObject json = new JSONObject();
                                json.put("subject", subject.getText().toString());
                                json.put("teacher", teacher.getText().toString());
                                json.put("term", term.getSelectedItem().toString());
                                json.put("id", id);
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                WebUtil.jsonOut(json, connection);
                                if (connection.getResponseCode() == 200)
                                    connection.getInputStream();
                            } catch (IOException | JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    Toast.makeText(SubjectDetail.this,"修改成功",Toast.LENGTH_SHORT).show();
                }
            });
        }
        else {
            comfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            try {
                                URL url = new URL(path + "add");
                                JSONObject json = new JSONObject();
                                json.put("subject", subject.getText().toString());
                                json.put("teacher", teacher.getText().toString());
                                json.put("term", term.getSelectedItem().toString());
                                json.put("id", id);
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                WebUtil.jsonOut(json, connection);
                            } catch (IOException | JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    Toast.makeText(SubjectDetail.this,"添加成功",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    public void uiinit(){
        subject = (EditText) findViewById(R.id.subject);
        teacher = (EditText) findViewById(R.id.teacher);
        term = (Spinner) findViewById(R.id.term);
        comfirm = (Button) findViewById(R.id.comfirm);
    }
}
